<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 插槽的使用 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDialogFn(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editData(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="del(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹窗组件 -->
    <add-depar ref="addDep" :show-dialog="showDialog" :add-user-id="userId" :user-code="userCode" @close-dialog="closeDialogFn" @updateData="getDataDepartments" />
  </div>
</template>

<script>
// 引入组织架构的发送请求
import { getDepartments, delDepartments } from '@/api/departments'
// 引入构造树形结构函数
import { transTreeData } from '@/utils/transTreeData'
// 引入组件
import addDepar from './add-depar/add-depar'
export default {
  // 注册组件
  components: {
    addDepar
  },
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 添加弹框是否显示
      showDialog: false,
      // 添加部门的id
      userId: null,
      // Code的数组
      userCode: []
    }
  },
  created() {
    // 发送请求
    this.getDataDepartments()
  },
  methods: {
    // 获取组织架构的数据
    async  getDataDepartments() {
      const res = await getDepartments()
      // console.log(res)
      this.userCode = res.depts.map(item => item.code)
      // console.log(this.userCode)
      this.list = transTreeData(res.depts)
      // console.log(this.list)
    },
    // 删除部门
    del(data) {
      // 跳出弹窗，咨询是否删除
      // console.log(data)
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 点击确认,发送请求
        await delDepartments(data.id)
        this.$message.success('删除成功')
        // 重新渲染
        this.getDataDepartments()
      }).catch((error) => {
        console.log(error)
      })
    },
    // 点击关闭弹窗
    closeDialogFn() {
      this.showDialog = false
    },
    // 添加部门
    addDialogFn(data) {
      this.showDialog = true
      console.log(data)
      this.userId = data
    },
    // 编辑部门
    editData(data) {
      // 跳出弹框
      this.showDialog = true
      this.userId = data
      // 通过refs调用子组件中的数据
      this.$refs.addDep.editDepartData(data.id)
    }
  }

}
</script>

<style lang="less" scoped>

</style>
